package br.pacote.negocio;

import java.sql.ResultSetMetaData;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.text.ParseException;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.text.MaskFormatter;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;


public class CadastrarNota extends JInternalFrame implements ActionListener
{
   private JLabel lbTitulo01;   
   private JLabel lbTitulo02;         
   private JLabel lbCodAluno; 
   private JLabel lbNota;         
   private JLabel lbMediaFinal;      
    
   
   private JTextField txtCodAluno;   
   private JTextField txtNotaA1;
   private JTextField txtNotaA2;
   private JTextField txtNotaA3;
   private JTextField txtNotaA4;
   private JTextField txtMediaFinal;
   
   
   private JButton btnGravar;  
   private JButton btnLimpar;
   private JButton btnPesquisar;
  
   public void Limpar()
   {
      txtCodAluno.setText("");      
      txtNotaA1.setText("A1");
      txtNotaA2.setText("A2");
      txtNotaA3.setText("A3");
      txtNotaA4.setText("A4");
      txtMediaFinal.setText("");
      
   }
   
   public CadastrarNota() throws ParseException
   {
      super("Cadastrar Nota");

      lbTitulo01 = new JLabel("SIGA - Sistema de Gestão Acadêmica");
      lbTitulo02 = new JLabel("Cadastrar Nota");
      lbCodAluno = new JLabel("Matrícula do aluno:");     
      lbNota = new JLabel("Nota:");
      lbMediaFinal = new JLabel("Média Final");      
      
      
      txtCodAluno = new JTextField("");      
      txtNotaA1 = new JTextField("A1");
      txtNotaA2 = new JTextField("A2");
      txtNotaA3 = new JTextField("A3");
      txtNotaA4 = new JTextField("A4");
      txtMediaFinal = new JTextField("");
      
      btnGravar = new JButton ("Gravar");      
      btnLimpar = new JButton ("Limpar");
      btnPesquisar = new JButton ("Pesquisar");
      
      setLayout(null);

      lbTitulo01.setBounds(280,-50,250,120);   
      lbTitulo02.setBounds(340,-30,150,120);   
      lbCodAluno.setBounds(10,70,200,20);       
      lbNota.setBounds(10,100,200,20);  
      lbMediaFinal.setBounds(10,130,200,20);  
      
            
      
      txtCodAluno.setBounds(120,70,100,20);      
      txtNotaA1.setBounds(120,100,50,20);  
      txtNotaA2.setBounds(170,100,50,20);  
      txtNotaA3.setBounds(220,100,50,20);  
      txtNotaA4.setBounds(270,100,50,20);  
      txtMediaFinal.setBounds(120,130,100,20);  
       
      btnGravar.setBounds(250,210,100,20);      
      btnLimpar.setBounds(400,210,100,20);
      btnPesquisar.setBounds(222,70,97,20);
                
          

      getContentPane().add(lbTitulo01);
      getContentPane().add(lbTitulo02);
      getContentPane().add(lbCodAluno);      
      getContentPane().add(lbNota);
      getContentPane().add(lbMediaFinal);
      
            
      
      getContentPane().add(txtCodAluno);      
      getContentPane().add(txtNotaA1);
      getContentPane().add(txtNotaA2);
      getContentPane().add(txtNotaA3);
      getContentPane().add(txtNotaA4);      
      getContentPane().add(txtMediaFinal);      
      
      getContentPane().add(btnGravar);      
      getContentPane().add(btnLimpar);
      getContentPane().add(btnPesquisar);
           
            
      btnGravar.addActionListener(this);      
      btnLimpar.addActionListener(this);
      btnPesquisar.addActionListener(this);
      
      setBounds(0,0,800,300);
      setVisible(true);
      setClosable(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);     
   }


   public void actionPerformed(ActionEvent e)
   {
       if(e.getSource()==btnPesquisar)
       {
         if(!txtCodAluno.getText().equals(""))
         {
                      
                                   
                                     String url="jdbc:mysql://localhost/siga";
                                     String driver="com.mysql.jdbc.Driver";
                                     String usuario= "root";
                                     String senha="root";

                                     Connection conexao = null;
                                     Statement sentenca = null;                                     
                                     ResultSet resultSet = null;
                                                                          
                                     
                                       try
                                       {   
                                           Class.forName(driver);
                                           conexao = DriverManager.getConnection(url,usuario,senha);
                                           
                                           sentenca = (Statement) conexao.createStatement();                                                                                                                               
                                                                                       
                                           resultSet = sentenca.executeQuery("Select codHistorico_escolar,nota1, nota2, nota3, nota4,media_final"
                                                   + " from historico_escolar h"
                                                   + " inner join aluno a"
                                                   + " on"
                                                   + " h.Aluno_idAluno=a.idAluno and a.idAluno=h.Aluno_idAluno"
                                                   + " where"
                                                   + " a.idAluno="+txtCodAluno.getText()+";");                                                                                     
                                                                                                                                                                            
                                           String verifica = null;
                                           
                                           Vector registro = null;
                                           
                                           while(resultSet.next())
                                           {
                                               registro = new Vector();
                                               registro.add(resultSet.getDouble(1));  
                                               
                                               verifica=String.valueOf(resultSet.getInt("codHistorico_escolar"));
                                               txtNotaA1.setText(String.valueOf(resultSet.getDouble("nota1")));
                                               txtNotaA2.setText(String.valueOf(resultSet.getDouble("nota2")));
                                               txtNotaA3.setText(String.valueOf(resultSet.getDouble("nota3")));
                                               txtNotaA4.setText(String.valueOf(resultSet.getDouble("nota4")));
                                               txtMediaFinal.setText(String.valueOf(resultSet.getDouble("media_final")));                                               
                                           }    
                                           
                                           if(verifica==null)
                                           {
                                               JOptionPane.showMessageDialog(null,"Nenhum registro encontrado!");
                                               Limpar();
                                           }
                                                                                                                                     
                                       }catch(ClassNotFoundException ex)
                                        {
                                            JOptionPane.showMessageDialog(this,"Driver não localizado: "+ ex.getMessage());


                                        }catch(SQLException ex)
                                        {
                                            JOptionPane.showMessageDialog(this,"Erro de SQL:"+ ex.getMessage());

                                        }finally
                                        {
                                            try
                                            {
                                                sentenca.close();
                                                resultSet.close();
                                                conexao.close();
                                            }catch(SQLException ex)
                                             {
                                                 JOptionPane.showMessageDialog(this, "Erro de SQL: " +ex.getMessage());
                                             }
                                        }
                            
       }//fim do if(txtNome!=null)
       else
       {
           JOptionPane.showMessageDialog(null,"Informe a matrícula do aluno!");
       }
      }
       
      if(e.getSource()==btnGravar)
      {
         if(!txtCodAluno.getText().equals(""))
         {
             if((txtNotaA1.getText().equals("A1"))||(txtNotaA2.getText().equals("A2"))||(txtNotaA3.getText().equals("A3"))||(txtNotaA4.getText().equals("A4"))||(txtMediaFinal.getText().equals("")))
             {
                 JOptionPane.showMessageDialog(null,"É necessário pesquisar uma matrícula de aluno!");
             }else
              {
                 String url="jdbc:mysql://localhost/siga";
                 String driver="com.mysql.jdbc.Driver";
                 String usuario= "root";
                 String senha="root";

                 Connection conexao = null;
                 Statement sentenca = null;         

                   try
                   {   
                       Class.forName(driver);
                       conexao = DriverManager.getConnection(url,usuario,senha);
                       sentenca = conexao.createStatement();

                       if(txtNotaA1.getText()=="A1")
                       {
                           txtNotaA1.setText("0.0");
                       }
                       if(txtNotaA2.getText()=="A2")
                       {
                           txtNotaA2.setText("0.0");
                       }
                       if(txtNotaA3.getText()=="A3")
                       {
                           txtNotaA3.setText("0.0");
                       }
                       if(txtNotaA4.getText()=="A4")
                       {
                           txtNotaA4.setText("0.0");
                       }

                       sentenca.execute("UPDATE `siga`.`historico_escolar` SET"
                               + " `nota1`="+txtNotaA1.getText()+",`nota2`="+txtNotaA2.getText()+""
                               + " ,`nota3`="+txtNotaA3.getText()+",`nota4`="+txtNotaA4.getText()+""
                               + " ,`media_final`="+txtMediaFinal.getText()+""
                               + " WHERE `Aluno_idAluno`="+txtCodAluno.getText()+";");                                                        

                   }catch(ClassNotFoundException ex)
                    {
                        JOptionPane.showMessageDialog(this,"Driver não localizado: "+ ex.getMessage());


                    }catch(SQLException ex)
                    {
                        JOptionPane.showMessageDialog(this,"Erro de SQL:"+ ex.getMessage());

                    }finally
                    {
                        try
                        {
                            sentenca.close();                      
                            conexao.close();
                        }catch(SQLException ex)
                         {
                             JOptionPane.showMessageDialog(this, "Erro de SQL: " +ex.getMessage());
                         }
                    }
         
         }//fim do if ()
       }//fim do if(!txtCodAluno.getText().equals(""))
       else
       {
           JOptionPane.showMessageDialog(null,"Informe a matrícula do aluno!");
       }
      } 
       
      if(e.getSource()==btnLimpar)
      {
         Limpar();
      }
   }
}
